package com.sf.fy.uav.applet.banner.service.impl;

import com.sf.fy.uav.applet.banner.service.SysBannerService;
import com.sf.fy.uav.applet.config.DelayQueue;
import com.sf.fy.uav.common.enums.SettingStatusEnum;
import com.sf.fy.uav.common.enums.ServicePrefixEnum;
import com.sf.fy.uav.common.request.operate.SysBannerAddRequest;
import com.sf.fy.uav.common.request.operate.SysBannerEditRequest;
import com.sf.fy.uav.common.utils.BeanUtil;
import com.sf.fy.uav.common.utils.RedisPrefixKey;
import com.sf.fy.uav.entity.Banner;
import com.sf.fy.uav.mapper.BannerMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * @author LTQ
 */
@Service
public class SysBannerServiceImpl implements SysBannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Autowired
    private DelayQueue delayQueue;

    @Override
    public void add(SysBannerAddRequest sysBannerAddRequest) {
        Banner banner = BeanUtil.copyProperties(sysBannerAddRequest, Banner.class);
        banner.setDeleted(false);
        Date startTime = sysBannerAddRequest.getStartTime();
        Date endTime = sysBannerAddRequest.getEndTime();
        Date date = new Date();
        if(date.getTime()>startTime.getTime()){
            throw new RuntimeException("开始时间不能小于当前时间");
        }
        if (startTime.getTime() > endTime.getTime()) {
            throw new RuntimeException("开始时间不能大于等于结束时间");
        }

        if(StringUtils.isNotBlank(sysBannerAddRequest.getJumpPath())&&ObjectUtils.isEmpty(sysBannerAddRequest.getJumpType())){
                throw new RuntimeException("有跳链必须选择类型!");
        }

        if (startTime.getTime() > date.getTime()) {
            //还没开始
            banner.setStatus(SettingStatusEnum.NOT_STARTED.getStatus());
            bannerMapper.insert(banner);
            //设置到期自动上架
            delayQueue.pushQueue(ServicePrefixEnum.EXPRESS_SYSTEM + RedisPrefixKey.BANNER_UP_COUNT_DOWN + banner.getId(), startTime.getTime() - date.getTime());
        } else {
            if (date.getTime() < endTime.getTime()) {
                banner.setStatus(SettingStatusEnum.ON_DISPLAY.getStatus());
                bannerMapper.insert(banner);
                //设置到期自动下架
                delayQueue.pushQueue(ServicePrefixEnum.EXPRESS_SYSTEM + RedisPrefixKey.BANNER_DOWN_COUNT_DOWN + banner.getId(), endTime.getTime() - date.getTime());
            } else {
                banner.setStatus(SettingStatusEnum.ENDED.getStatus());
                bannerMapper.insert(banner);
            }
        }
    }

    @Override
    public void edit(SysBannerEditRequest sysBannerEditRequest) {
        Banner banner = bannerMapper.selectById(sysBannerEditRequest.getId());
        if (ObjectUtils.isEmpty(banner)) {
            throw new RuntimeException("banner不存在");
        }
        if(StringUtils.isNotBlank(sysBannerEditRequest.getJumpPath())&&ObjectUtils.isEmpty(sysBannerEditRequest.getJumpType())){
            throw new RuntimeException("有跳链必须选择类型!");
        }
        Date startTime = sysBannerEditRequest.getStartTime();
        Date endTime = sysBannerEditRequest.getEndTime();
        Date date = new Date();
        if(!SettingStatusEnum.ON_DISPLAY.getStatus().equals(sysBannerEditRequest.getStatus())){
            if(date.getTime()>startTime.getTime()){
                throw new RuntimeException("开始时间不能小于当前时间");
            }
        }
        if (startTime.getTime() > endTime.getTime()) {
            throw new RuntimeException("开始时间不能大于等于结束时间");
        }
        if(StringUtils.isBlank(sysBannerEditRequest.getProcessedBy())){
            throw new RuntimeException("处理人不能为空");
        }
        banner.setProcessedBy(sysBannerEditRequest.getProcessedBy());
        banner.setName(sysBannerEditRequest.getName());
        banner.setStartTime(startTime);
        banner.setEndTime(endTime);
        banner.setUpdateTime(date);
        banner.setSort(sysBannerEditRequest.getSort());
        banner.setJumpPath(sysBannerEditRequest.getJumpPath());
        banner.setJumpType(sysBannerEditRequest.getJumpType());
        banner.setCover(sysBannerEditRequest.getCover());
        if (startTime.getTime() > date.getTime()) {
            //还没开始
            banner.setStatus(SettingStatusEnum.NOT_STARTED.getStatus());
            //设置自动上架
            delayQueue.pushQueue(ServicePrefixEnum.EXPRESS_SYSTEM + RedisPrefixKey.BANNER_UP_COUNT_DOWN + banner.getId(), startTime.getTime() - date.getTime());
        } else {
            if (date.getTime() < endTime.getTime()) {
                banner.setStatus(SettingStatusEnum.ON_DISPLAY.getStatus());
                //设置自动下架
                delayQueue.pushQueue(ServicePrefixEnum.EXPRESS_SYSTEM + RedisPrefixKey.BANNER_DOWN_COUNT_DOWN + banner.getId(), endTime.getTime() - date.getTime());
            } else {
                banner.setStatus(SettingStatusEnum.ENDED.getStatus());
            }
        }
        bannerMapper.updateById(banner);
    }
}
